package com.demoweb.model.dao;
//오류 있는데 일단 올림. 어차피 이 dao는 나만 쓸거같으니ㅠㅠ
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.demoweb.model.dto.Board;
import com.demoweb.model.dto.TetrisBoarddto;


public class TetrisscoreDao {
	public void insertTetrisscore(TetrisBoarddto score1) {
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
	
		try {
			Class.forName("com.mysql.jdbc.Driver");
			//2. 연결 생성
			conn = DriverManager.getConnection(
				"jdbc:mysql://127.0.0.1:3306/newdemoweb",	//url
				"devuser", "mysql");	
			String sql= 
				"INSERT INTO Tetrisscore " + 
				"(name, gamescore, gamelevel, row, regdate, regno, gameno) " +
				"VALUES (?, ?, ?, ?, ?, ?, ? )";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, score1.getName());
			pstmt.setLong(2, score1.getGamescore());
			pstmt.setInt(3, score1.getRow());
			pstmt.setDate(4, score1.getRegdate());
			pstmt.setLong(5, score1.getRegno());
			pstmt.setInt(6, score1.getGameno());
			pstmt.executeUpdate();
			
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try { rs.close(); } catch (Exception ex) { }
			try { pstmt.close(); } catch (Exception ex) { }
			try { conn.close(); } catch (Exception ex) { }
		}
	}
	public List<TetrisBoarddto> getRankList(int startRow, int count) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		ArrayList<TetrisBoarddto> boards = new ArrayList<TetrisBoarddto>();
		
		try {
			conn = ConnectionHelper.getConnection();
			StringBuilder sql = new StringBuilder(512);
			sql.append("SELECT name, score, row, regdate ")
			   .append("FROM Tetrisscore ")
			   .append("ORDER BY gamescore DESC, level DESC ")
			   .append("LIMIT ?, ?");
			pstmt = conn.prepareStatement(sql.toString());
			pstmt.setInt(1, startRow);
			pstmt.setInt(2, count);
			
			rs = pstmt.executeQuery();
			
//			while (rs.next()) {
//				Board Tetrisscore = new Board();
//				Tetrisscore.setName(rs.getString(1));
//				Tetrisscore.setGamescore(rs.getLong(2));
//				Tetrisscore.setRow(rs.getInt(3));
//				//board.setContent(rs.getString(4));
//				Tetrisscore.setRegdate(rs.getInt(4));
//				
//				boards.add(Tetrisscore);
//			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
		
			if (rs != null) try { rs.close(); } catch (Exception ex) {}
			if (pstmt != null) try { pstmt.close(); } catch (Exception ex) {}
			if (conn != null) try { conn.close(); } catch (Exception ex) {}
		}
		
		return boards;
	}

}

